package com.xgst.runningmanager

import CountdownTimer
import android.Manifest
import android.content.Intent
import android.text.TextUtils
import android.view.Gravity
import android.widget.FrameLayout
import androidx.activity.viewModels
import com.blankj.utilcode.util.FragmentUtils
import com.blankj.utilcode.util.PermissionUtils
import com.blankj.utilcode.util.TimeUtils
import com.google.android.material.snackbar.Snackbar
import com.xgst.baselib.BaseActivity
import com.xgst.baselib.utils.showToast
import com.xgst.lovevideocall.viewmodel.RongViewModel
import com.xgst.runningmanager.bean.DataR
import com.xgst.runningmanager.databinding.ActivityMainBinding
import com.xgst.runningmanager.fragment.LookFragment
import com.xgst.runningmanager.fragment.MineFragment

class MainActivity :BaseActivity<ActivityMainBinding>(){

    private val rongViewModel: RongViewModel by viewModels()
    private val eventTypes = arrayOf("设备异常", "网络异常", "物业问题", "群众聚集", "暗访", "其他")

    override fun init() {
        initRong()
        initPermission()
        val xunFragment = LookFragment()
        val mineFragment = MineFragment()

        FragmentUtils.add(supportFragmentManager,mineFragment,R.id.flContent,true)
        FragmentUtils.add(supportFragmentManager,xunFragment,R.id.flContent,false)

        binding.rbLook.setOnClickListener {
            binding.rbLook.setBackgroundResource(R.drawable.shape_selected)
            binding.rbMine.setBackgroundResource(R.drawable.shape_unselect)
            FragmentUtils.showHide(xunFragment,mineFragment)
        }

        binding.rbMine.setOnClickListener {
            binding.rbMine.setBackgroundResource(R.drawable.shape_selected)
            binding.rbLook.setBackgroundResource(R.drawable.shape_unselect)
            FragmentUtils.showHide(mineFragment,xunFragment)
        }

        initLoop()
    }
    private var data:DataR? = null
    private fun initLoop() {
        CountdownTimer(initialSeconds = 60*60*24, timeInterval = 10*1000, onTick = {
            rongViewModel.loopMsg{
                data = it
                showTopSnackbar(it.title)
            }
        }).start()
    }

    private fun initPermission() {
        PermissionUtils.permission(
            Manifest.permission.CAMERA,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.RECORD_AUDIO)
            .request()
    }

    private fun initRong() {
        rongViewModel.loginAndConnect("xgst111", onConnect = {

        })
    }

    private fun showTopSnackbar(message: String) {
        val snackbar = Snackbar.make(findViewById(android.R.id.content), message, Snackbar.LENGTH_LONG)
        val view = snackbar.view
        view.setOnClickListener {
            snackbar.dismiss()
            data?.let { item ->
                val intent = Intent(this,OrderDetailActivity::class.java)
                if(!TextUtils.isEmpty(item.category)){
                    val i = item.category.toInt()
                    if(i < eventTypes.size){
                        intent.putExtra("category",eventTypes[i])
                    }
                }
                intent.putExtra("tvUseName",item.submitUser)
                intent.putExtra("tvQ",item.description)
                intent.putExtra("etJ",item.resolveDescription)
                intent.putExtra("attachFile",item.attachFile)
                intent.putExtra("title",item.title)
                intent.putExtra("tvTime", TimeUtils.millis2String(item.submitTime))
                startActivity(intent)
            }
        }
        val params = view.layoutParams as FrameLayout.LayoutParams
        params.gravity = Gravity.TOP
        view.layoutParams = params

        snackbar.show()
    }
}